﻿using GalaSoft.MvvmLight.Messaging;
using NicoView;
using NicoView.Model.Service.Errors;
using NicoView.Properties;
using NicoView.SingleProcess;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Deployment.Application;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using NicoView.Model;
using NicoView.View;
using System.Reflection;
using NicoView.ViewModel;
using NicoView.Speech;
using NicoView.ViewModelLocators;



namespace NicoView
{
    /// <summary>
    /// App.xaml の相互作用ロジック
    /// </summary>
    public partial class App : Application
    {
        
       

        protected override void OnStartup(StartupEventArgs e)
        {

            this.Resources["settings"] = NicoView.Properties.Settings.Default;
            NicoView.Properties.Resources.Culture = CultureInfo.GetCultureInfo("ja-jp");

            this.ViewModelLocator.Main.Title = NicoView.Properties.Resources.NicoView + " " + GetVersion();


           
            using (var mutex = CreateMutex())
            {
                if (mutex.WaitOne(TimeSpan.FromSeconds(5)))
                {
                    InternalApplication.InitializeAsync().GetAwaiter().GetResult();
                }
                else
                {
                    Environment.Exit(-1);
                }
            }

            NicoViewSpeechController.Current.Initialize();

        }

        internal Settings Settings
        {
            get
            {
                return (Settings)this.Resources["settings"];
            }
        }

        internal Resources Resource
        {
            get
            {
                return (Resources)this.Resources["resources"];
            }
        }

        internal ViewModelLocator ViewModelLocator
        {
            get
            {
                return (ViewModelLocator)this.Resources["locator"];
            }
        }

        public NicoViewApplication InternalApplication
        {
            get
            {
                return NicoViewApplication.Current;
            }
        }



        public Mutex CreateMutex()
        {
            return new Mutex(false, this.ProductName + "{475A7188-6DC1-41EB-8987-D0B4D86462ED}");
        }

        public string ProductName
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Name;
            }
        }







        protected override  void OnExit(ExitEventArgs e)
        {
            
            using (var mutex = CreateMutex())
            {
                
                Settings.Default.Save();
                NicoViewApplication.Current.FinalizeAsync().GetAwaiter().GetResult();
                
                
            }
            
        }







        private static string versionMessage;
        public static string GetVersion()
        {
            if (ApplicationDeployment.IsNetworkDeployed)
            {
                if (string.IsNullOrEmpty(versionMessage))
                {
                    var version = ApplicationDeployment.CurrentDeployment.CurrentVersion;
                    versionMessage = version.Major.ToString() + "." + version.Minor.ToString() + "." + version.Build.ToString() + "." + version.Revision.ToString();
                }
                return versionMessage;
            }
            else
            {
                return "";
            }
        }






        private void Appllication_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {

            ExceptionShowWindow.Show(e.Exception);
            ErrorManager.SaveExceptionAsync(e.Exception).GetAwaiter().GetResult();
            e.Handled = true;
            this.Shutdown();

        }
        

        
    }
}
